package com.hk.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hk.domain.Dept;
import com.hk.domain.User;
import com.hk.mapper.DeptMapper;
import com.hk.mapper.UserMapper;
import com.hk.service.DeptService;
import com.hk.vo.DeptAndUserVo;
import com.hk.vo.TreeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void updateByDeptId(Dept dept) {
        log.info("开始修改部门信息");
        LambdaUpdateWrapper<Dept> wrapper = new LambdaUpdateWrapper<>();
        //没改的信息就没传，所有需要加上判断
        wrapper.set(StringUtils.isNotEmpty(dept.getParentName()), Dept::getParentName, dept.getParentName())
                .set(StringUtils.isNotEmpty(dept.getDeptName()), Dept::getDeptName, dept.getDeptName())
                .set(dept.getOrderNum() != null, Dept::getOrderNum, dept.getOrderNum())
                .set(StringUtils.isNotEmpty(dept.getLeader()), Dept::getLeader, dept.getLeader())
                .set(StringUtils.isNotEmpty(dept.getPhone()), Dept::getPhone, dept.getPhone())
                .set(StringUtils.isNotEmpty(dept.getEmail()), Dept::getEmail, dept.getEmail())
                .set(dept.getStatus() != null, Dept::getStatus, dept.getStatus())
                .eq(Dept::getDeptId, dept.getDeptId());                //根据id进行修改
        deptMapper.update(wrapper);
    }

    /**
     * 根据id删除部门信息
     * @param deptId 部门id
     */
    @Override
    public void deleteById(Integer deptId) throws Exception{
        //查询该部门是否有下属部门，有的话则不能删除
        List<Dept> depts = deptMapper.selectList(Wrappers.<Dept>lambdaQuery().eq(Dept::getParentId, deptId));
        if (depts.size() > 0) {
           //有下属部门，不能删除
            throw new Exception("该部门有下属部门，不能删除");
        }
        //没有下属部门，删除
        deptMapper.deleteById(deptId);
    }

    @Override
    public void selectByDeptId(Integer deptId) {

        HashSet<Integer> list = new HashSet<>();
        ArrayList<Integer> list3 = new ArrayList<>();
        while (true){
            ArrayList<Integer> list2 = new ArrayList<>();
            list2.addAll(list3);
            list3.clear();

            if (true){
                list2.add(deptId);
               list2=deptMapper.selectListId(list2);
            }


        }



        /*select(integers);*/

//  查询上级部门id相同的人，看有没有再遍历
    }
/*    void select(ArrayList<Integer> integers){
        ArrayList<DeptAndUserVo> deptAndUserVo = new ArrayList<>();
//        查询当前部门的人 通过部门depId 找人 死循环一直到没有部门没有人
        while (true) {
            for (Integer integer : integers) {
                LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(User::getDeptId,integer);
                List<User> userList = userMapper.selectList(wrapper);
//        判断集合是否为空，不为空就遍历，为空就去下级部门查
                if (userList.size()!=0){
                    for (User user : userList) {
                        DeptAndUserVo d = new DeptAndUserVo();
                        BeanUtils.copyProperties(user,d);
                        //                查询user的部门
                        Dept dept = deptMapper.selectOne(Wrappers.<Dept>lambdaQuery().eq(Dept::getDeptId, user.getDeptId()));
                        d.setDept(dept);
                        log.info("存储");
                        deptAndUserVo.add(d);
                    }
                }

            }



        }
    }*/


}
